﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SAM_Edu.control;
using AForge.Video.DirectShow;
using AForge.Video;
using SAM_Edu.form;

namespace SAM_Edu
{
    public partial class frmCreateUser : Form
    {
        public frmCreateUser()
        {
            InitializeComponent();
        }
        CreateUser cr = new CreateUser();

        private void btnCreate_Click(object sender, EventArgs e)
        {
            string username = tbUserName.Text;
            string password = tbPassword.Text;
            string email = tbEmail.Text;
            string securityqn = tbSecurityQn.Text;
            string securityans = tbSecurityAns.Text;
            int mobileNo = int.Parse(tbMobileNo.Text);
            int countrycode = int.Parse(tbCountryCode.Text);
            int areacode = int.Parse(tbAreaCode.Text);
            string firstname = tbfirstname.Text;
            string middlename = tbMiddleName.Text;
            string lastname = tbLastName.Text;
            string othername = tbOtherName.Text;
            string securityQn = tbSecurityQn.Text;
            string securityAns = tbSecurityAns.Text;
            string rfidno = tbrfidno.Text;
            Image img = pictureBox1.Image;
            CreateUser.RequestCreateUserAccount(firstname, middlename, lastname, othername, email, countrycode, areacode, mobileNo, username, password, securityQn, securityAns, rfidno, img);
        }

        private FilterInfoCollection videoDevices;
        private VideoCaptureDevice videoDevice;
        private VideoCapabilities[] videoCapabilities;
        private VideoCapabilities[] snapshotCapabilities;

       
        private void frmCreateUser_Load(object sender, EventArgs e)
        {
            // enumerate video devices
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            if (videoDevices.Count != 0)
            {
                // add all devices to combo
                foreach (FilterInfo device in videoDevices)
                {
                    devicesCombo.Items.Add(device.Name);
                }
            }
            else
            {
                devicesCombo.Items.Add("No DirectShow devices found");
            }

            devicesCombo.SelectedIndex = 0;

            EnableConnectionControls(true);


            if (triggerButton.Enabled == false)
            {
                MessageBox.Show("Your webcam doesn't support snapshot, please select another webcam or use another webcam!");
            }
        }
        private void EnableConnectionControls(bool enable)
        {
            devicesCombo.Enabled = enable;
            videoResolutionsCombo.Enabled = enable;
            snapshotResolutionsCombo.Enabled = enable;
            
            disconnectButton.Enabled = !enable;
            triggerButton.Enabled = (!enable) && (snapshotCapabilities.Length != 0);
            
        }

        private void frmCreateUser_FormClosing(object sender, FormClosingEventArgs e)
        {
            Disconnect();
        }

        private void devicesCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            Disconnect();
            if (videoDevices.Count != 0)
            {
                videoDevice = new VideoCaptureDevice(videoDevices[devicesCombo.SelectedIndex].MonikerString);
                EnumeratedSupportedFrameSizes(videoDevice);
                if (videoDevice != null)
                {
                    if ((videoCapabilities != null) && (videoCapabilities.Length != 0))
                    {
                        videoDevice.DesiredFrameSize = (Size)videoResolutionsCombo.SelectedItem;
                    }

                    if ((snapshotCapabilities != null) && (snapshotCapabilities.Length != 0))
                    {
                        videoDevice.ProvideSnapshots = true;
                        videoDevice.DesiredSnapshotSize = (Size)snapshotResolutionsCombo.SelectedItem;
                        videoDevice.SnapshotFrame += new NewFrameEventHandler(videoDevice_SnapshotFrame);
                    }

                    EnableConnectionControls(false);

                    videoSourcePlayer.VideoSource = videoDevice;
                    videoSourcePlayer.Start();
                }
            }

        }
        private void EnumeratedSupportedFrameSizes(VideoCaptureDevice videoDevice)
        {
            this.Cursor = Cursors.WaitCursor;

            videoResolutionsCombo.Items.Clear();
            snapshotResolutionsCombo.Items.Clear();

            try
            {
                videoCapabilities = videoDevice.VideoCapabilities;
                snapshotCapabilities = videoDevice.SnapshotCapabilities;

                foreach (VideoCapabilities capabilty in videoCapabilities)
                {
                    if (!videoResolutionsCombo.Items.Contains(capabilty.FrameSize))
                    {
                        videoResolutionsCombo.Items.Add(capabilty.FrameSize);
                    }
                }

                foreach (VideoCapabilities capabilty in snapshotCapabilities)
                {
                    if (!snapshotResolutionsCombo.Items.Contains(capabilty.FrameSize))
                    {
                        snapshotResolutionsCombo.Items.Add(capabilty.FrameSize);
                    }
                }

                if (videoCapabilities.Length == 0)
                {
                    videoResolutionsCombo.Items.Add("Not supported");
                }
                if (snapshotCapabilities.Length == 0)
                {
                    snapshotResolutionsCombo.Items.Add("Not supported");
                }

                videoResolutionsCombo.SelectedIndex = 0;
                snapshotResolutionsCombo.SelectedIndex = 0;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void connectButton_Click(object sender, EventArgs e)
        {
            
        }

        private void disconnectButton_Click(object sender, EventArgs e)
        {
            Disconnect();
        }
        private void Disconnect()
        {
            if (videoSourcePlayer.VideoSource != null)
            {
                // stop video device
                videoSourcePlayer.SignalToStop();
                videoSourcePlayer.WaitForStop();
                videoSourcePlayer.VideoSource = null;

                if (videoDevice.ProvideSnapshots)
                {
                    videoDevice.SnapshotFrame -= new NewFrameEventHandler(videoDevice_SnapshotFrame);
                }

                EnableConnectionControls(true);
            }
        }

        private void triggerButton_Click(object sender, EventArgs e)
        {
            if ((videoDevice != null) && (videoDevice.ProvideSnapshots))
            {
                videoDevice.SimulateTrigger();
            }
        }
        private void videoDevice_SnapshotFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Console.WriteLine(eventArgs.Frame.Size);

            ShowSnapshot((Bitmap)eventArgs.Frame.Clone());
        }
        private void ShowSnapshot(Bitmap snapshot)
        {
            if (InvokeRequired)
            {
                Invoke(new Action<Bitmap>(ShowSnapshot), snapshot);
            }
            else
            {
                /*if (snapshotForm == null)
                {
                    snapshotForm = new frmSnapShot();
                    snapshotForm.MdiParent = this.MdiParent;
                    snapshotForm.StartPosition = FormStartPosition.Manual;
                    snapshotForm.Location = new System.Drawing.Point(10, 10);
                    snapshotForm.FormClosed += new FormClosedEventHandler(frmSnapShot_FormClosed);
                    snapshotForm.Show();
                }
                */
                pictureBox1.Image = snapshot;
            }
        }
        private void frmSnapShot_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }
    }
}
